<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "gky_msg".
 *
 * @property string $id
 * @property string $msgtype
 * @property integer $isforward
 * @property string $forwardid
 * @property string $title
 * @property string $description
 * @property string $content
 * @property string $addman
 * @property string $addmanid
 * @property string $addtime
 * @property integer $isfinish
 * @property string $sendtime
 * @property string $meetingtime_start
 * @property string $meetingtime_end
 * @property string $meetingplace
 * 
 * @property Upfile[] $dupfiles
 * 
 */
class Msg extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'gky_msg';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['isforward', 'forwardid', 'isfinish'], 'integer'],
            [['meetingtime_start', 'meetingtime_end', 'sendtime'], 'safe'],
            [['msgtype', 'addman', 'addmanid'], 'string', 'max' => 64],
            [['title'], 'string', 'max' => 128],
            [['description'], 'string', 'max' => 3000],
            [['content'], 'string', 'max' => 4000],
            [['meetingplace'], 'string', 'max' => 512],
//            修改验证 添加 时间
            [['addtime'], 'safe'],
//            数据验证
            [['msgtype', 'title', 'content'], 'required'],
        ];
    }

    public function getMsgUsers() {

        return $this->hasMany(GkyMsgUser::className(), ['msgid' => 'id']);
    }

    public function getQyhUser(){

        return $this->hasOne(GkyQyhUser::className(),['id'=>'addmanid']);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'msgtype' => '消息发送类型',
            'isforward' => '是否属于转发',
            'forwardid' => '转发消息编号',
            'title' => '标题',
            'description' => '摘要',
            'content' => '内容',
            'addman' => '发送人',
            //'addmanid' => 'Addmanid',
            'addtime' => '添加时间',
            'isfinish' => '是否完成',
            'sendtime' => '发送时间',
            'meetingtime_start' => '开始时间',
            'meetingtime_end' => '结束时间',
            'meetingplace' => '会议地点',
        ];
    }

    public function afterSave($insert, $changedAttributes) {
        parent::afterSave($insert, $changedAttributes);

        //保存附件
        $upfile = new GkyMsgDocument();
        $path = Yii::$app->request->post('files');
        $filename = Yii::$app->request->post('fileNames');
        $filestatus = Yii::$app->request->post('fileStatus');
        for ($i = 0; $i < count($filestatus); $i++) {
            if ($upfile->save()) {
                $upfile->msgid = $this->id;
                if ($filestatus[$i] == 1) {
                    $upfile->filepath = $path[$i];
                    $upfile->filename = $filename[$i];
                    $upfile->save();
                }
            }
        }
    }

    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
//            //验证是否选择了接收对象
//            //得到从页面post过来的发送对象
//            $users = Yii::$app->request->post('selects2');
//            if (!$users) {
//                return;
//            }
//
//            //添加修改人的信息 由于这部分没有必要自己填写 所以直接获取登录人的信息  在保存的时候设置就好
//            if (isset(Yii::$app->user->identity->id)) {
//                $id = Yii::$app->user->identity->id;
//                //添加人的id
//                $this->addmanid = $id;
//                //添加人的姓名
//                $name = GkyUser::find()->where(['id' => $id])->one()->username;
//                $this->addman = $name;
//            }
            //添加时间、发送时间
            $time = date("Y-m-d H:i:s", time());
            $this->addtime = $time;
            $this->sendtime = $time;
            return true;
        } else {
            return false;
        }
    }

    public function getUpfiles() {
        return $this->hasMany(GkyMsgDocument::className(), ['msgid' => 'id']);
    }

}
